package main

import "sync"
/**
等全部通道消息处理结束(closed),可将已完成通道设置为nil。
这样他就会被阻塞，不在被select选中
 */
func main() {
	var wg sync.WaitGroup
	wg.Add(2)

	a,b := make(chan int),make(chan int)

	go func() {
		defer wg.Done()

		for {
			select {
			case x,ok := <-a:
				if !ok {
					a =nil
					break
				}
				println("a",x)
			case x,ok := <-b:
				if !ok {
					b =nil
					break
				}
				println("b",x)
			}

			if a == nil && b== nil {
				return
			}
		}
	}()

	go func() {
		defer wg.Done()
		defer close(a)
		for i := 0; i < 3; i++ {
			a <- i
		}
	}()

	go func() {
		defer wg.Done()
		defer close(b)
		for i := 0; i < 5; i++ {
			b <- i *10
		}
	}()
	wg.Wait()

}
